package com.dyzx.hbb.modules.wms.controller;


import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.wms.entity.WmsProductCategory;
import com.dyzx.hbb.modules.wms.entity.WmsProductSupplier;
import com.dyzx.hbb.modules.wms.query.WmsProductSupplierQuery;
import com.dyzx.hbb.modules.wms.service.WmsProductSupplierService;

import com.dyzx.hbb.modules.wms.vo.WmsProductSupplierUVO;
import com.dyzx.hbb.modules.wms.vo.WmsProductSupplierVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 供应商管理控制器
 * <p>处理仓库管理系统中产品供应商的创建、查询和管理业务逻辑
 * <p>主要职责：
 * <ul>
 * <li>提供供应商的基础CRUD操作</li>
 * <li>支持按名称、联系人、电话、邮箱、地址和状态等条件筛选供应商信息</li>
 * <li>展示供应商的详细信息，包括联系人、联系方式和地址等</li>
 * <li>提供供应商的简易列表接口</li>
 * </ul>
 * <p>设计意图：为仓库管理模块提供完整的供应商数据管理接口，
 * 确保供应商信息的完整性和一致性，支持供应链管理
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "供应商管理", description = "供应商相关接口")
@RestController
@RequestMapping("/wms/product/supplier")
public class WmsProductSupplierController extends BaseValidController<
        WmsProductSupplierService,
        WmsProductSupplier,
        WmsProductSupplierQuery,
        WmsProductSupplierVO,
        WmsProductSupplierUVO
        > {

    @Autowired
    private WmsProductSupplierService service;

    @Override
    protected WmsProductSupplierService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsProductSupplier> queryWrapper(WmsProductSupplierQuery query) {
        MyLambdaQueryWrapper<WmsProductSupplier> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(WmsProductSupplier::getId)
                .select(WmsProductSupplier::getName)
                .select(WmsProductSupplier::getStatus)
                .select(WmsProductSupplier::getContact)
                .select(WmsProductSupplier::getPhone)
                .select(WmsProductSupplier::getEmail)
                .select(WmsProductSupplier::getAddress)
                .select(WmsProductSupplier::getCreateDate);
        queryWrapper.orderByDesc(WmsProductSupplier::getCreateDate);

        if (query != null) {
            queryWrapper.likeRight(query.getName() != null, WmsProductSupplier::getName, query.getName())
                    .likeRight(StringUtils.isNotBlank(query.getContact()), WmsProductSupplier::getContact, query.getContact())
                    .likeRight(StringUtils.isNotBlank(query.getPhone()), WmsProductSupplier::getPhone, query.getPhone())
                    .likeRight(StringUtils.isNotBlank(query.getEmail()), WmsProductSupplier::getEmail, query.getEmail())
                    .likeRight(StringUtils.isNotBlank(query.getAddress()), WmsProductSupplier::getAddress, query.getAddress())
                    .eq(query.getStatus() != null, WmsProductSupplier::getStatus, query.getStatus());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsProductSupplier> queryInfoWrapper() {
        MyLambdaQueryWrapper<WmsProductSupplier> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(WmsProductSupplier::getId)
                .select(WmsProductSupplier::getName)
                .select(WmsProductSupplier::getStatus)
                .select(WmsProductSupplier::getContact)
                .select(WmsProductSupplier::getPhone)
                .select(WmsProductSupplier::getEmail)
                .select(WmsProductSupplier::getAddress)
                .select(WmsProductSupplier::getCreateDate);
        queryWrapper.orderByDesc(WmsProductSupplier::getCreateDate);
        return queryWrapper;
    }

    @Override
    protected WmsProductSupplier addValid(WmsProductSupplierVO entity) {
        return null;
    }

    @Override
    protected WmsProductSupplier editValid(WmsProductSupplierUVO entity) {
        return null;
    }

    @Operation(summary = "简易列表")
    @GetMapping("/mini_list")
    public Result<List<Map<String, Object>>> miniList(
    ) {
        MyLambdaQueryWrapper<WmsProductSupplier> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(WmsProductSupplier::getId, WmsProductSupplier::getName);
        wrapper.select(WmsProductSupplier::getName).as("label");
        wrapper.select(WmsProductSupplier::getId).as("value");
        wrapper.orderByDesc(WmsProductSupplier::getId);


        List<Map<String, Object>> list = getBaseService().listMaps(wrapper);
        return list.isEmpty() ? Result.error(404, "暂无数据") : Result.ok(list);
    }
}